package com.nio.c2_Path;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @version No one is coming, unless you change yourself!!!
 * @author： pang-yun
 * @date： 2021-05-31 15:44
 */

//walkFileTree   遍历文件夹

public class WalkFileTree {
    public static void main(String[] args) throws IOException {
        //==AtomicInteger则通过一种线程安全的加减操作接口==   使用int 是局部变量   会报错
        AtomicInteger dirCount= new AtomicInteger();
        AtomicInteger fileCount= new AtomicInteger();

        Files.walkFileTree(Paths.get("G:\\photo"),new SimpleFileVisitor<Path>(){
            @Override      //进入文件夹之前
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("=====>"+dir);
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("====>"+file);
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }
        });

        System.out.println("dirCount:  "+dirCount);
        System.out.println("fileCount:  "+fileCount);
    }
}
